/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utility;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mirza
 *
 * How to use it :
 * String[] listField = {"NIM", "PASSWORD"};
 * TableGenerator.generateTable(jTableUser,resultSet, listField);
 *
 **/
public class TableGenerator {

    public static void generateTable(JTable table, ResultSet tableValue, String[] listField) throws SQLException {
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

        while (tableModel.getRowCount() > 0) {
            tableModel.removeRow(0);
        }

        while (tableValue.next()) {
            String[] listValue = new String[listField.length];
            for (int i = 0; i < listField.length; i++)
                listValue[i] = tableValue.getString(listField[i]);
            tableModel.addRow(listValue);
        }
    }
}
